<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:buy:add']"
        >新增</el-button>
      </el-col>
     </el-row>
  <div class="layoutStyle">
    <div class="leftStyle">
      <el-table v-loading="loading" :data="buyList" @selection-change="handleSelectionChange">
      <!-- <el-table-column label="类型标识" align="center" prop="ptype"/> -->
      <el-table-column label="零件编号" align="center" prop="partNumber" />
      <el-table-column label="零件名称" align="center" prop="partName" />
      <el-table-column label="数量" align="center" prop="quantity" />
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="总金额" align="center" prop="totalPrice" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:buy:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:buy:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

     <!-- 添加或修改外购对话框 -->

    <div v-if="open" width="500px" style="margin-top:25px">
      <el-form ref="buyRef" :model="form" :rules="rules" label-width="120px">
        <!-- <el-form-item label="产品或项目ID" prop="pid">
          <el-input v-model="form.pid" placeholder="请输入产品或项目ID" />
        </el-form-item> -->
        <el-form-item label="零件编号" prop="partNumber">
          <el-input v-model="form.partNumber" placeholder="请输入零件编号" />
        </el-form-item>
        <el-form-item label="零件名称" prop="partName">
          <el-input v-model="form.partName" placeholder="请输入零件名称" />
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
        <!-- <el-form-item label="总金额" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入总金额" />
        </el-form-item> -->
        <!-- <el-form-item label="全局唯一标识" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入全局唯一标识" />
        </el-form-item> -->
      </el-form>
    <div style="text-align: center; height: 80px;">
      <ul>
      <li style="height: 35px;">数量 * 单价 = 总金额</li>
      <li>{{form.quantity?form.quantity:0}} * {{form.price?form.price:0}} = {{form.quantity*form.price}}</li>
    </ul>
    </div>
    <div style="text-align: right;">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
    </div>
   </div>
    <div class="rightStyle">
      
    </div>
    </div>
  </div>
</template>

<script setup name="Buy">
import { listBuy, getBuy, delBuy, addBuy, updateBuy } from "@/api/system/buy"

const { proxy } = getCurrentInstance()
const { partid } =defineProps(["partid"])
const buyList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)

const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    pid: partid,
  },
  rules: {
    // pid: [
    //   { required: true, message: "产品或项目ID不能为空", trigger: "blur" }
    // ],
    ptype: [
      { required: true, message: "类型标识不能为空", trigger: "change" }
    ],
    partNumber: [
      { required: true, message: "零件编号不能为空", trigger: "blur" }
    ],
    partName: [
      { required: true, message: "零件名称不能为空", trigger: "blur" }
    ],
    quantity: [
      { required: true, message: "数量不能为空", trigger: "blur" }
    ],
    price: [
      { required: true, message: "单价不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)


/** 查询外购列表 */
function getList() {
  loading.value = true
  listBuy(queryParams.value).then(response => {
    buyList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    ptype: null,
    partNumber: null,
    partName: null,
    quantity: null,
    price: null,
}
  proxy.resetForm("buyRef")
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加外购"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getBuy(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改外购"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["buyRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        form.value.totalPrice = null
        updateBuy(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        form.value.pid = partid
        addBuy(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除外购编号为"' + _ids + '"的数据项？').then(function() {
    return delBuy(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}
getList()
</script>

<style scoped>
.layoutStyle{
  display: flex;
  flex-direction: row;
  .leftStyle{
    width: 70%;
   }
  .rightStyle{
    width: 30%;
  }
  ul, ol {
  list-style-type: none; /* 移除所有列表的默认标记 */
  padding-left: 0; /* 移除默认的左缩进 */
  margin-left: 0;
  }
  ol {
    height: 140px;
  }
}
</style>